package com.qf.tenement.tenement_rent.service.impl;


import com.alibaba.fastjson.JSON;
import com.qf.tenement.tenement_rent.Renting;
import com.qf.tenement.tenement_rent.dao.SearchDao;
import com.qf.tenement.tenement_rent.dao.SearchsDao;
import com.qf.tenement.tenement_rent.service.SearchService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
public class SearchSercicesImpl implements SearchService {
    @Resource
    private SearchDao searchDao;
    @Resource
    private SearchsDao searchsDao;
    @Resource
    private RestHighLevelClient restHighLevelClient;
    @Override
    public Iterable<Renting> findAll(String key) {
        PageRequest request = PageRequest.of(0, 5);
        return   searchsDao.findAllByRoomTypeOrRentWayOrXqNameOrAddressOrHouseTypeOrTowardsOrRoomLightOrSource(key,key,key,key,key,key,key,key,request);
    }

    @Override
    public boolean paeseConten() throws IOException {
        List<Renting> list = searchDao.selectAll();

        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");
        for (Renting renting : list) {
            bulkRequest.add(new IndexRequest("sms-films-index").source(JSON.toJSONString(renting), XContentType.JSON));
        }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulk.hasFailures();
    }
}
